{extend name="layout:base" /}
{block name="page_style"}
<link rel="stylesheet" href="{$cdn_root}/static/index/css/data_y_m_d.css?v={$static_version}">
{/block}
{block name="page_content"}
<!--属于专题专栏-->
<section class="declare-do">
    <div class="center">
        <div class="else-title">
            申报项目名称：{$article.title}
        </div>
        <div class="form-group" data-type="formData">
            <div class="form-item must" data-name="title">
                <div class="form-left">
                    论文名称：
                </div>
                <div class="form-right">
                    <input type="text" name="title">
                </div>
            </div>
            <div class="form-item must" data-name="attachment_name">
                <div class="form-left">
                    上传资料：
                </div>
                <div class="form-right file">
                    <input type="text" readonly name='attachment_name'>
                    <button id="doUp">上传</button>
                    <p class="prompt">仅限上传doc、docx、xls、xlsx、pdf、zip格式文件。</p>
                </div>
            </div>
            <div class="form-item must" data-name="user_name">
                <div class="form-left">
                    姓名：
                </div>
                <div class="form-right">
                    <input type="text" name="user_name" value="{$apply.user_name}">
                </div>
            </div>
            <div class="form-item must" data-name="gender">
                <div class="form-left">
                    性别：
                </div>
                <div class="form-right radio">
                    <div class="sel-group">
                        {volist name="gender_list" id="name"}
                        <?php if($apply['gender']==$key): ?>
                        <div class="sel-item on">
                            <div class="sel-bg">
                                <div class="sel-on"></div>
                                <input type="radio" name="gender" value="{$key}" data-value="{$key}">
                            </div>
                            {$name}
                        </div>
                        <?php else: ?>
                        <div class="sel-item">
                            <div class="sel-bg">
                                <div class="sel-on"></div>
                                <input type="radio" name="gender" value="{$key}" data-value="{$key}">
                            </div>
                            {$name}
                        </div>
                        <?php endif; ?>
                        {/volist}
                    </div>
                </div>
            </div>
            <div class="form-item must" data-name="birthday">
                <div class="form-left">
                    出生年月：
                </div>
                <div class="form-right date">
                    <input type="text" class="date_picker" name="birthday" value="{$apply.birthday}"
                           readonly="readonly">
                    <i class="icon icon-date"></i>
                </div>
            </div>
            <div class="form-item must" data-name="id_num">
                <div class="form-left">
                    身份证号：
                </div>
                <div class="form-right">
                    <input type="text" name="id_num">
                </div>
            </div>
            <div class="form-item must" data-name="education">
                <div class="form-left">
                    最高学历：
                </div>
                <div class="form-right sel">
                    <select name="education">
                        <option value="">--请选择--</option>
                        <?php $edus = list_field_education(); ?>
                        {volist name="edus" id="name"}
                        <?php if(isset($apply['education']) && $apply['education']==$key): ?>
                        <option value="{$key}" selected="selected">{$name}</option>
                        <?php else: ?>
                        <option value="{$key}">{$name}</option>
                        <?php endif; ?>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="form-item must" data-name="pro_title">
                <div class="form-left">
                    职称：
                </div>
                <div class="form-right sel">
                    <select name="pro_title">
                        <option value="">--请选择--</option>
                        <?php $pros = list_field_pro_title(); ?>
                        {volist name="pros" id="name"}
                        <?php if(isset($apply['pro_title']) && $apply['pro_title']==$key): ?>
                        <option value="{$key}" selected="selected">{$name}</option>
                        <?php else: ?>
                        <option value="{$key}">{$name}</option>
                        <?php endif; ?>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="form-item must" data-name="email">
                <div class="form-left">
                    邮箱：
                </div>
                <div class="form-right email">
                    <input type="text" name="email" value="{$apply.email}">
                </div>
            </div>
            <div class="form-item must" data-name="mobile">
                <div class="form-left">
                    手机号：
                </div>
                <div class="form-right phone">
                    <input type="text" name="mobile" value="{$apply.mobile}">
                </div>
            </div>
            <div class="form-item must" data-name="college_name">
                <div class="form-left">
                    学校名称：
                </div>
                <div class="form-right">
                    <input type="text" name="college_name" value="{$apply.college_name}">
                </div>
            </div>
            <div class="form-item must" data-name="college_type">
                <div class="form-left">
                    学校类型：
                </div>
                <div class="form-right sel">
                    <select name="education">
                        <option value="">--请选择--</option>
                        <?php $types = list_field_college_type(); ?>
                        {volist name="types" id="name"}
                        <?php if(isset($apply['college_type']) && $apply['college_type']==$key): ?>
                        <option value="{$key}" selected="selected">{$name}</option>
                        <?php else: ?>
                        <option value="{$key}">{$name}</option>
                        <?php endif; ?>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="form-item must" data-name="college_no">
                <div class="form-left">
                    机构标识码：
                </div>
                <div class="form-right">
                    <input type="text" name="college_no">
                </div>
            </div>
            <div class="form-item must" data-name="college_depart">
                <div class="form-left">
                    二级院校：
                </div>
                <div class="form-right">
                    <input type="text" name="college_depart">
                </div>
            </div>
            <div class="controls">
                <button class="form-group-submit">提交</button>
                <button class="else"><a href="{:url('column/read', ['id' => $article['id']])}">取消</a></button>
            </div>
        </div>
        <form method="post" id="file-form" style="display: none" enctype="multipart/form-data">
            <!--<input type="hidden" name="csrf_token">-->
            <input type="file" id="upload" name="upfile" accept="application/zip,application/x-rar-compressed,application/pdf,application/vnd.ms-excel,application/msword,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.presentationml.presentation">
        </form>
        <p>资料一经提交不可修改，提交前请仔细核对信息及上传资料！</p>
    </div>
</section>
{/block}
{block name="page_js"}
<script src="{$cdn_root}/static/index/js/data_y_m_d.js?v={$static_version}"></script>
<script type="text/javascript">
    $(document).ready(function () {
        //代替执行
        $('#doUp').on('click', function () {
            $('#upload').click();
        });
        $("button.else").on('click', function(){
            var $href = $(this).find('a');
            if($href.length){
                $href[0].click();
            }
        });
        $('.date_picker').date_input();
        //监测上传
        $('#upload').on('change', function () {
            let arr = $(this).val().split('\\');
            $('.form-item .form-right.file input').val(arr[arr.length - 1]);
            let formMsg = new FormData($('#file-form')[0]);//获取文件的格式
            $.ajax({
                url: "{:url('upload/simditor')}",
                data: formMsg,
                method: 'POST',
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    $('#upload').val('');
                    formData.attachment_path = data.file_path;
                    formData.attachment_name = arr[arr.length - 1];
                },
                error: function (data) {
                    console.log(data);
                }
            });
        });
        let formData = {
            title: '',
            attachment_path: '',
            attachment_name: '',
            user_name: '',
            gender: '',
            birthday: '',
            id_num: '',
            education: '',
            pro_title: '',
            email: '',
            mobile: '',
            college_name: '',
            college_type: '',
            college_no: '',
            college_depart: '',
        };
        $('.form-group-submit').on('click', function () {
            let father = $(this).closest('.form-group'), canSend = true;
            father.find('.form-item').each(function () {
                if ($(this).hasClass('must')) {
                    //单选按钮
                    if ($(this).find('.form-right').hasClass('radio')) {
                        if ($(this).find('.sel-item.on')[0] !== undefined) {
                            formData[`${$(this).attr('data-name')}`] = $(this).find('.sel-item.on input').val();
                        }
                        else {
                            $(this).addClass('error');
                            alertMsg(`${$(this).find('.form-left').text()}不能为空`);
                            canSend = false;
                            return false;
                        }
                    }
                    //下拉框
                    else if ($(this).find('.form-right').hasClass('sel')) {
                        if ($(this).find('select').val() === '') {
                            $(this).addClass('error');
                            alertMsg(`${$(this).find('.form-left').text()}不能为空`);
                            canSend = false;
                            return false;
                        }
                        else {
                            formData[`${$(this).attr('data-name')}`] = $(this).find('select').val();
                        }
                    }
                    //邮箱
                    else if ($(this).find('.form-right').hasClass('email')) {
                        if ($(this).find('input').val() === '') {
                            $(this).addClass('error');
                            alertMsg(`${$(this).find('.form-left').text()}不能为空`);
                            canSend = false;
                            return false;
                        }
                        else {
                            let reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                            if (reg.test($(this).find('input').val())) {
                                formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                            }
                            else {
                                alertMsg('邮箱格式不正确');
                                canSend = false;
                                return false;
                            }
                        }
                    }
                    //手机号
                    else if ($(this).find('.form-right').hasClass('phone')) {
                        if ($(this).find('input').val() === '') {
                            $(this).addClass('error');
                            alertMsg(`${$(this).find('.form-left').text()}不能为空`);
                            canSend = false;
                            return false;
                        }
                        else {
                            let reg = /^((1([3-5]|[7-9])[0-9]))\d{8}$/;
                            if (reg.test($(this).find('input').val())) {
                                formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                            }
                            else {
                                alertMsg('手机号格式不正确');
                                canSend = false;
                                return false;
                            }
                        }
                    }
                    //上面添加特殊验证
                    else {
                        //通用检查
                        if ($(this).find('input').val() === '') {
                            $(this).addClass('error');
                            alertMsg(`${$(this).find('.form-left').text()}不能为空`);
                            canSend = false;
                            return false;
                        }
                        else {
                            if ($(this).attr('data-minLength') !== undefined) {
                                if ($(this).find('input').val().length < parseInt($(this).attr('data-minLength'))) {
                                    alertMsg(`${$(this).find('.form-left').text()}最短${$(this).attr('data-minLength')}位`);
                                    $(this).addClass('error');
                                    canSend = false;
                                    return false;
                                }
                            }
                            if ($(this).attr('data-maxLength') !== undefined) {
                                if ($(this).find('input').val().length > parseInt($(this).attr('data-maxLength'))) {
                                    alertMsg(`${$(this).find('.form-left').text()}最长${$(this).attr('data-maxLength')}位`);
                                    $(this).addClass('error');
                                    canSend = false;
                                    return false;
                                }
                            }
                            formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                        }
                    }
                }
                //非必填选项
                else {
                    //单选按钮
                    if ($(this).find('.form-right').hasClass('radio')) {
                        if ($(this).find('.sel-item.on')[0] !== undefined) {
                            formData[`${$(this).attr('data-name')}`] = $(this).find('.sel-item.on input').val();
                        }
                    }
                    //下拉框
                    else if ($(this).find('.form-right').hasClass('sel')) {
                        if ($(this).find('select').val() !== '') {
                            formData[`${$(this).attr('data-name')}`] = $(this).find('select').val();
                        }
                    }
                    //邮箱
                    else if ($(this).find('.form-right').hasClass('email')) {
                        if ($(this).find('input').val() !== '') {
                            let reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                            if (reg.test($(this).find('input').val())) {
                                formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                            }
                            else {
                                alertMsg('邮箱格式不正确');
                                canSend = false;
                                return false;
                            }
                        }
                    }
                    //手机号
                    else if ($(this).find('.form-right').hasClass('phone')) {
                        if ($(this).find('input').val() !== '') {
                            let reg = /^((1([3-5]|[7-9])[0-9]))\d{8}$/;
                            if (reg.test($(this).find('input').val())) {
                                formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                            }
                            else {
                                alertMsg('手机号格式不正确');
                                canSend = false;
                                return false;
                            }
                        }
                    }
                    //上面添加特殊验证
                    else {
                        if ($(this).find('input').val() !== '') {
                            if ($(this).attr('data-minLength') !== undefined) {
                                if ($(this).find('input').val().length < parseInt($(this).attr('data-minLength'))) {
                                    alertMsg(`${$(this).find('.form-left').text()}最短${$(this).attr('data-minLength')}位`);
                                    $(this).addClass('error');
                                    canSend = false;
                                    return false;
                                }
                            }
                            if ($(this).attr('data-maxLength') !== undefined) {
                                if ($(this).find('input').val().length > parseInt($(this).attr('data-maxLength'))) {
                                    alertMsg(`${$(this).find('.form-left').text()}最长${$(this).attr('data-maxLength')}位`);
                                    $(this).addClass('error');
                                    canSend = false;
                                    return false;
                                }
                            }
                            formData[`${$(this).attr('data-name')}`] = $(this).find('input').val();
                        }
                    }
                }
            });
            //返回true表示全通过，可以发送数据
            if (canSend) {
                $.ajax({
                    url: "{:url('create', ['article_id' => $article['id']])}",
                    data: formData,
                    method: 'POST',
                    success: function (data) {
                        if (data.code) {
                            location.href = data["url"];
                        } else {
                            alertMsg(data['msg']);
                        }
                    },
                    error: function (data) {
                        failMsg("系统繁忙，请稍后再试！");
                    }
                });
            }
        });
    });
</script>
{/block}